%*****************************************************
%** This is a file to generate predicted values for **
%** four hypothetical cases in Section 2.1          **
%** Created by Min-Hua Huang, Jan 25, 2012          **
%*****************************************************
clear; clc; format shortG;
load HansfordGomez_Data.mat;
code=HansfordGomez_Data(:,2);
y=HansfordGomez_Data(:,27);
grandmeany=mean(y);
n=size(y,1); 
x0=[HansfordGomez_Data(:,26) HansfordGomez_Data(:,3) HansfordGomez_Data(:,25) HansfordGomez_Data(:,29) HansfordGomez_Data(:,11:23)];
grandmeanx0=mean(x0);
m=size(x0,2);
obs=(1:n)';
replicate=[y x0 code obs];
replicate=sortrows(replicate,19);
%****** Replicate xtreg ******
dmean=dmean1(replicate,19);
mean4a=[(replicate(:,1:18)-dmean) replicate(:,19:20)];
mean4a=sortrows(mean4a,20);
y=mean4a(:,1)+grandmeany;
xw=ones(n,m)*-999;
for i=1:1:m
    xw(:,i)=mean4a(:,i+1)+grandmeanx0(i);
end
x=[ones(n,1) xw];
olsb=(x'*x)\(x'*y);
% **** precited values of the four hypothetical cases ****
load hypocase.mat; load district.mat;
n=size(district,1);
x1=ones(n,17)*-999; 
x2=ones(n,17)*-999;
x3=ones(n,17)*-999;
x4=ones(n,17)*-999;
for i=1:1:n
    x1(i,:)=hypocase(1,3:19)-district(i,3:19)+grandmeanx0;
    x2(i,:)=hypocase(2,3:19)-district(i,3:19)+grandmeanx0;
    x3(i,:)=hypocase(3,3:19)-district(i,3:19)+grandmeanx0;
    x4(i,:)=hypocase(4,3:19)-district(i,3:19)+grandmeanx0;
end    
predict1=max([ones(n,1) x1]*olsb);
predict2=min([ones(n,1) x2]*olsb);
predict3=max([ones(n,1) x3]*olsb); 
predict4=min([ones(n,1) x4]*olsb);
fprintf('\nExamples of Theoretical Out-Of-Bound Violations, using the model from Hansford and Gomez (2010)\n')
fprintf('\nHypothetical Case 1\n')
fprintf('Partisan composition=88.98%%, Turnout=100%%, Yr64=1, GOP_incumbent=1\n')
fprintf('Greatest Predicted Value=% 5.2f%%\n',predict1);
fprintf('\nHypothetical Case 2\n')
fprintf('Partisan composition=10.14%%, Turnout=20.37%%, Yr72=1, GOP_incumbent=0\n')
fprintf('Least Predicted Value=% 5.2f%%\n',predict2);
fprintf('\nHypothetical Case 3\n')
fprintf('Partisan composition=60%%, Turnout=60%%, Yr64=1, GOP_incumbent=1\n')
fprintf('Greatest Predicted Value=% 5.2f%%\n',predict3);
fprintf('\nHypothetical Case 4\n')
fprintf('Partisan composition=40%%, Turnout=40%%, Yr72=1, GOP_incumbent=0\n')
fprintf('Least Predicted Value=% 5.2f%%\n\n',predict4);





